package com.kye.express.business.order.scan_code_address;

import android.text.TextUtils;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.ErrorView;
import com.kye.express.contract.base.view.IView;
import com.kye.express.contract.base.view.LoadingView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.order.scan_code_order.ScanCodeOrderAddress;
import com.kye.model.bean.user.User;
import com.kye.model.datasource.OrderDataSource;

import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public interface ScanCodeAddressEditContract {
    interface ScanCodeAddressEditView extends IView, LoadingView, ErrorView {
        void onResult(ScanCodeOrderAddress address);

    }

    class ScanCodeAddressEditPresenter extends BasePresenter<ScanCodeAddressEditView> {

        private OrderDataSource mOrderDataSource;

        @Override
        public void bind(ScanCodeAddressEditView view) {
            super.bind(view);
            mOrderDataSource = new OrderDataSource();
        }

        public void submit(String province, String city, String county, String address, String contacts, String contactsPhone, boolean defult, boolean sendAddress, boolean save, String guid) {
            final ScanCodeOrderAddress addressData = new ScanCodeOrderAddress(county, contacts, contactsPhone, defult, address, sendAddress ? 1 : 2, province, null, city, guid);

            if (save) {
                UserSession.getInstance()
                        .getUserForRx()
                        .firstOrError()
                        .flatMap(new Function<User, SingleSource<OpenApiResult<Object>>>() {
                            @Override
                            public SingleSource<OpenApiResult<Object>> apply(User user) throws Exception {
                                Single<OpenApiResult<Object>> openApiResultSingle;

                                addressData.setPhone(user.getPhone());

                                if (TextUtils.isEmpty(guid)) {
                                    openApiResultSingle = mOrderDataSource.addScanCodeAddress(addressData);
                                } else {
                                    openApiResultSingle = mOrderDataSource.modifyScanCodeAddress(addressData);
                                }
                                return openApiResultSingle;
                            }
                        })
                        .compose(getView().bindToLifecycleDestroy())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new OpenApiResultObserver<Object>() {
                            @Override
                            public void onLoading(Disposable d) {
                                getView().onLoading();
                            }

                            @Override
                            public void onSuccess(OpenApiResult<Object> result, Object o) {
                                getView().onResult(addressData);
                            }

                            @Override
                            public void onApiError(OpenApiResult<Object> result, int code, String msg) {
                                getView().onError(msg);
                            }

                            @Override
                            public void onOtherError(Throwable e) {
                                getView().onError();
                            }

                            @Override
                            public void onLoaded() {
                                getView().onLoaded();
                            }
                        });
            } else {
                UserSession.getInstance()
                        .getUserForRx()
                        .firstOrError()
                        .map(new Function<User, ScanCodeOrderAddress>() {
                            @Override
                            public ScanCodeOrderAddress apply(User user) throws Exception {
                                addressData.setPhone(user.getPhone());
                                return addressData;
                            }
                        })
                        .compose(getView().bindToLifecycleDestroy())
                        .subscribe(new Consumer<ScanCodeOrderAddress>() {
                            @Override
                            public void accept(ScanCodeOrderAddress addressData) throws Exception {
                                getView().onResult(addressData);
                            }
                        });
            }
        }
    }
}
